//
//  ScanNavigationController.m
//  Staff
//
//  Created by jing zhao on 3/30/16.
//
//
#import "VideoPlaybackViewController.h"
#import "ZBarReaderViewController.h"
#import "ZBarReaderView.h"
#import "ViewController.h"
#import "ScanNavigationController.h"
#import "NotificationDefine.h"
#import "QRCodeViewController.h"
#import "QRCodeScanOverlayerDiscover.h"

#import <Cordova/CDV.h>

@interface ScanNavigationController () <ZBarReaderDelegate>
@property (strong,nonatomic)NSMutableArray *arrayReleaseViewControllerB;
@end

@implementation ScanNavigationController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    QRCodeViewController *scanReader = [self getTheQrController];
    self.viewControllers =@[scanReader];
    self.arrayReleaseViewControllerB = [NSMutableArray array];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(check) name:NotificationScanCheck object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(cancel) name:NotificationScanCancel object:nil];
    
}

-(void)dealloc{
    [self.arrayReleaseViewControllerB removeAllObjects];
    [super dealloc];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    // Dispose of any resources that can be recreated.
}
-(void)cancel{
    [self dismissViewControllerAnimated:NO completion:NULL];
    CDVPluginResult *pluginResult =[CDVPluginResult
                                    resultWithStatus: CDVCommandStatus_ERROR
                                    messageAsString: @"Failed"];
    [[NSNotificationCenter defaultCenter]postNotificationName:NotificationScanResult object:pluginResult];
}

-(void)check{
    UIViewController *viewController = [self.viewControllers objectAtIndex:0];
    [self.arrayReleaseViewControllerB addObject:viewController];
    if (self.arrayReleaseViewControllerB && [self.arrayReleaseViewControllerB count]>3){
        UIViewController *viewControllerOne =[self.arrayReleaseViewControllerB objectAtIndex:0];
        [self.arrayReleaseViewControllerB removeObject:viewControllerOne];
    }

    if ([viewController isKindOfClass:[ZBarReaderViewController class]]) {
        VideoPlaybackViewController *slideMenu = [self getTheVideoPlaybackViewController];
        self.viewControllers =@[slideMenu];
        
    }else{
        [[NSNotificationCenter defaultCenter]postNotificationName:NotificationPouseAr object:nil];
        QRCodeViewController *scanReader = [self getTheQrController];
        self.viewControllers =@[scanReader];
    }
    
}
-(QRCodeViewController*)getTheQrController{
    QRCodeViewController *scanReader = [QRCodeViewController new];
    scanReader.readerDelegate = self;
    scanReader.supportedOrientationsMask = ZBarOrientationMaskAll;
    scanReader.showsCameraControls = NO;
    scanReader.showsZBarControls = NO;
    scanReader.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
    QRCodeScanOverlayerDiscover* overlayer = [[QRCodeScanOverlayerDiscover alloc] initWithFrame:CGRectZero];
    //    overlayer.delegate = self;
    [scanReader setCameraOverlayView:overlayer];
    
    return scanReader;
    
}
-(VideoPlaybackViewController *)getTheVideoPlaybackViewController{
    Class vcClass = NSClassFromString(@"VideoPlaybackViewController");
    id vc = [[vcClass alloc]  initWithNibName:nil bundle:nil];
    return vc;
}
#pragma mark - ZBarReaderDelegate

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results) break; // get the first result
    
    NSLog(@"symbol.data = %@", symbol.data);
    [self dismissViewControllerAnimated:YES completion:NULL];
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:symbol.data];
    [[NSNotificationCenter defaultCenter]postNotificationName:NotificationScanResult object:pluginResult];
    
}

- (void) imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
    [self dismissViewControllerAnimated:YES completion:NULL];
    CDVPluginResult *pluginResult =[CDVPluginResult
                                    resultWithStatus: CDVCommandStatus_ERROR
                                    messageAsString: @"cancelled"];
    [[NSNotificationCenter defaultCenter]postNotificationName:NotificationScanResult object:pluginResult];
    
}

- (void) readerControllerDidFailToRead:(ZBarReaderController*)reader withRetry:(BOOL)retry
{
    [self dismissViewControllerAnimated:YES completion:NULL];
    CDVPluginResult *pluginResult =[CDVPluginResult
                                    resultWithStatus: CDVCommandStatus_ERROR
                                    messageAsString: @"Failed"];
    [[NSNotificationCenter defaultCenter]postNotificationName:NotificationScanResult object:pluginResult];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
